package com.ancun;

import ancun.seal.servlet.SealClient;
import ancun.seal.util.FileUtil;
import ancun.seal.util.StringUtil;
import org.junit.Test;

import java.io.File;
import java.util.UUID;

//import org.junit.*;

/**
 * PDF自动化签章
 *
 */
public class SealAutoPdfTest extends ServletBaseTest{


//    public SealAutoPdfTest() throws Exception {
//        sealAutoPdfTest();
//    }
    @Test
    public void sealAutoPdfTest() throws Exception {
        /** HTTP方式 */
        //调用接口url拼接
        String url = super.url + SealClient.SLASH + SealClient.PDF_SEAL_SERVLET;
        SealClient sealClient = new SealClient(url);
        //参数赋值
        String type = "2";      //2：坐标 3：关键字
        String sealCode = "cfca_3_1543453841318";//"cfca_2_1543457804359901";
        String sealPassword = "123456";
        String sealPerson = "()（）江苏江南农村商业银行股份有限公司江苏江南农村商业银行股份有限公司";
        String sealLocation = "()（）江苏省常州市天宁区和平中路 413 号";
        String sealResson = "()（）江苏江南农村商业银行股份有限公司签合同";
        String blankFieldName = "FieldName:edb4964a-6792-4882-8ef3-27bc76929724";

        // 支持（1）单页，（1,2,3,4）多页,（1-3）区间，（0）所有页
        String page = "0";
        String lX = "0";
        String lY = "0";
        String locationStyle = "";

        // 签章错误
//        byte[] str = "签名位置".getBytes();
//        byte[] str = "深圳市前海公证处公证申请表".getBytes();
//        byte[] str = "1614820522950".getBytes();
        byte[] str = "深圳市前海公证处公证申请表".getBytes();
        String keyword = new String(str);//"14.1"; //"或盖章";
//        String sealStrategyXML = "<Request><Head/><Body><SerialVersionUID/>" + "<Type>" + type + "</Type>" + "<SealCode>" + sealCode
//                + "</SealCode><SealPassword>" + sealPassword + "</SealPassword><Page>" + page + "</Page><SealPerson>" + sealPerson
//                + "</SealPerson><SealLocation>" + sealLocation + "</SealLocation>" + "<SealResson>" + sealResson + "</SealResson>" + "<BlankFieldName>" + blankFieldName +
//                "</BlankFieldName>" + "<LX>" + lX + "</LX><LY>" + lY
//                + "</LY><Keyword>" + keyword + "</Keyword>" + "</Body></Request>";

        String sealStrategyXML = "<Request>" + "<Type>" + type + "</Type>" + "<SealCode>" + sealCode
                + "</SealCode><SealPassword>" + sealPassword + "</SealPassword><Page>" + page + "</Page><SealPerson>" + sealPerson
                + "</SealPerson>" + "<LocationStyle>" + locationStyle + "</LocationStyle>" + "<SealLocation>" + sealLocation + "</SealLocation>"
                + "<SealResson>" + sealResson + "</SealResson>" + "<LX>" + lX + "</LX><LY>" + lY
                + "</LY><Keyword>" + keyword + "</Keyword>" + "</Request>";

        long time1 = System.currentTimeMillis();
        //指定生成的pdf模板路径
//        String pdfURL = super.baseSources + "身份核验报告.pdf";
//        String pdfURL = super.baseSources + "16148205234207d2f0.pdf";
        String pdfURL = super.baseSources + "037022200111_08119199.pdf";
        byte[] pdf = FileUtil.getBytesFromFile(new File(pdfURL));

//        String pdfURL = "/home/hancm/tdbank-sealserver/doc/podofo签章使用mupdf库函数崩溃-可能因为内部库冲突-已解决-使用命令行查询-已修改mupdf的freeType库解决.pdf&/home/hancm/1.pdf";
//        byte[] pdf = pdfURL.getBytes();

        byte[] sealedPdf = null;
        sealedPdf = sealClient.sealAutoPdf(pdf, sealStrategyXML);
        long time2 = System.currentTimeMillis();
        System.out.println("Time: " + (time2-time1));

        if (sealedPdf.length < 255) {
            System.out.println("签章路径: " + new String(sealedPdf));
        } else if (StringUtil.checkResultDataValid(sealedPdf)) {
            //指定生成PDF存储路径
            String savePath = super.targetSources + UUID.randomUUID() + ".pdf";
            FileUtil.wirteDataToFile(savePath, sealedPdf);
            System.out.println("com.ancun.SealAutoPdfTest(Servlet) ok and save data to \"" + savePath + "\'");
        } else {
            System.out.println("com.ancun.SealAutoPdfTest(Servlet) wrong " + new String(sealedPdf, "UTF-8"));
        }
        System.out.println(sealedPdf.length);
    }
}


